<?php
/**
 *
 * User: lang@vip.deyi.com
 * Date: 2018/1/8
 * Time: 11:02
 */

namespace Apps\Acp\Controllers;


use Apps\Common\Constant\BusinessConstant;
use Apps\Common\Controller\AcpBaseController;
use Apps\Weixin\Models\WxConfig;
use Apps\Weixin\Services\WxConfigService;
use Deyi\Functions\PageFunction;

class ConfigController extends AcpBaseController
{
    public function indexAction()
    {
        $code = $this->getParamValue('code', '');

        $page = PageFunction::getPageAndOffset($this->getParamValue('page', 1, 'int'), BusinessConstant::PAGE_SIZE);

        $conditions = [];

        if ($code) {
            $conditions['attrcode%'] = $code;
        }

        $total = WxConfig::getInstance()->countListByConditions($conditions);
        $list = [];
        if ($total) {
            $data = WxConfig::getInstance()->getListByConditions($conditions, $page['offset'], $page['size'], ' attrcode ASC ');

            foreach ($data as $value){
                if( $info = str_replace("trading_", '', (str_replace("view_", "", $value->attrcode) ))){
                    $info = explode(',', $info);
                    $value->attrcode = $info[0].'---'.$info[1];
                }
                $list[] = $value;
            }
        }

        $this->view->setVars([
            'list' => $list,
            'pageHtml' => PageFunction::getPageHtml(
                $page['page'],
                $total,
                $page['size'],
                parse_url($this->request->getURI())['path'],
                [
                    'code' => $code
                ],
                4
            ),
            'code' => $code,
            'nowtime' => time()
        ]);

    }

    public function switchAction()
    {
        $id = $this->getParamValue('id', null, 'int');
        $enable = $this->getParamValue('enable', 0, 'int');

        WxConfig::getInstance()->updateByFields(['enable' => $enable, 'service' => BusinessConstant::SERVICE_SPECIFY_CODE], ['id' => $id]);

        WxConfigService::getInstance()->getConfigByService(BusinessConstant::SERVICE_SPECIFY_CODE, true);

        parent::renderSuccessJson();
    }

    public function deleteAction()
    {
        $id = $this->getParamValue('id', null, 'int');

        WxConfig::getInstance()->deleteByFields([
            'service' => BusinessConstant::SERVICE_SPECIFY_CODE,
            'id' => $id
        ]);

        WxConfigService::getInstance()->getConfigByService(BusinessConstant::SERVICE_SPECIFY_CODE, true);

        parent::renderSuccessJson();
    }

    public function editAction()
    {
        $id = $this->getParamValue('id', 0, 'int');

        $data = null;
        if ($id) {
            $data = WxConfig::getInstance()->getOneByFields([
                'service' => BusinessConstant::SERVICE_SPECIFY_CODE,
                'id' => $id
            ]);
            $attrcode= $data->attrcode;
            $data->type = "trading_";
            $data->from = 0;
            $data->to = 0;
            if((strpos($attrcode,"trading_") !== false) && ($attrcode = str_replace("trading_", "", $attrcode)) ){
                $info  = explode(',',$attrcode);
                $data->type = "trading_";
                $data->from = $info[0];
                $data->to = $info[1];
            }
            if( (strpos($attrcode,"view_") !== false) && ( $attrcode = str_replace("view_", "", $attrcode))){
                $info  = explode(',',$attrcode);
                $data->type = "view_";
                $data->from = $info[0];
                $data->to = $info[1];
            }
        }

        $this->view->setVars(
            [
                'data' => $data,
                'id' => $id,
                'imgsrc' => static::$_config->project->imageurl,
                'active' => $data ? $data->attrtype : 1
            ]
        );
    }

    public function saveAction()
    {
        $id = $this->getParamValue('id', 0, 'int');
        $attrType = $this->getParamValue('attrtype');
        $type = $this->getParamValue('type');
        $from = $this->getParamValue('from');
        $to = $this->getParamValue('to');
        $attrVal = $this->getParamValue('attrval');
        $attrDesc = $this->getParamValue('attrdesc');
        $enable = $this->getParamValue('enable', 0, 'int') ? 1 : 0;

        $attrCode =$type.$from.','.$to;
        $values = [
            'service' => BusinessConstant::SERVICE_SPECIFY_CODE,
            'attrtype' => $attrType,
            'attrcode' => $attrCode,
            'attrval' => $attrVal,
            'attrdesc' => $attrDesc,
            'enable' => $enable,
            'dateline' => time()
        ];

        $list = WxConfig::getInstance()->getListByConditions(
            ['service' => BusinessConstant::SERVICE_SPECIFY_CODE, 'attrcode' => $attrCode], 0, BusinessConstant::MAX_DATA_SIZE
        );
        foreach ($list as $v) {
            if (($v->id != $id)) {
                static::errorThrow('已经存在相同配置代号code');
            }
        }

        if ($id) {
            WxConfig::getInstance()->updateByFields($values, ['id' => $id]);
        } else {
            WxConfig::getInstance()->insertByFields($values);
        }

        WxConfigService::getInstance()->getConfigByService(BusinessConstant::SERVICE_SPECIFY_CODE, true);

        parent::renderSuccessJson();
    }

}